#include "StdAfx.h"

#include "Player.h"
#include "camera/Camera.h"
#include "input/InputManager.h"
#include "terrain/Terrain.h"
#include "obj/ObjContainer.h"
#include "core/Game.h"

Player::Player() : camera(new Camera), moveSpeed(50.0f), rotSpeed(5.0f),
    terrain(Terrain::getInstance()), input(InputManager::getInstance())
{
	glm::dvec2 window_center = glm::dvec2(Game::windowDim.x / 2, Game::windowDim.y / 2);
	input->setMousePos(window_center);
}

Player::~Player(){}

void Player::draw(float dt, bool toScreen)
{
    handleInput(dt);

	float terrainHeight = terrain->sample(glm::vec2(-camera->eye.x, -camera->eye.z));
	camera->eye.y = - terrainHeight - 5.0f;
}

void Player::handleInput(float dt)
{
    /* Keyboard move */
    if (input->isKeyPressed(keyboard_w)
		) 
        camera->moveForward(moveSpeed*dt);
    else if (input->isKeyPressed(keyboard_s)) 
        camera->moveForward(-moveSpeed*dt);
    if (input->isKeyPressed(keyboard_a))
        camera->moveLeft(moveSpeed*dt);
    else if ( input->isKeyPressed(keyboard_d))
        camera->moveLeft(-moveSpeed*dt);

	
	glm::dvec2 window_center = glm::dvec2(Game::windowDim.x / 2, Game::windowDim.y / 2);
	glm::dvec2 mouse_pos = input->getMousePos();
	glm::dvec2 mouse_delta = mouse_pos - window_center;
	camera->rotateY(dt*(float)mouse_delta.x*rotSpeed);
	camera->rotateX(dt*(float)mouse_delta.y*rotSpeed);
	input->setMousePos(window_center);
}

glm::mat4& Player::getViewMatrix() { return camera->getViewMatrix(); }
